package DataStructureAndAlgorithm.CSP.Day05;
import java.util.Scanner;
//网络延时
//树的直径
//用邻接表存点
public class AcWing_3215 {
    static int N = 20010;
    static int M = N;
    static int n,m;
    static int res = 0;
    static int[] e = new int[N];
    static int[] ne = new int[N];
    static int[] h = new int[N];
    static int idx = 0;
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        //初始化头结点
        for (int i = 0; i < N; i++){
            h[i] = -1;
        }
        //读入交换机的连接点，并把相连的两个交换机建边
        for (int i = 2; i <= n; i++){
            int p = in.nextInt();
            add(p,i);
        }
        //读入电脑的连接点，并把相连的两个设备建边
        for (int i = n + 1; i <= n + m; i++){
            int p = in.nextInt();
            add(p,i);
        }
        DFS(1);
        System.out.print(res);

    }

    static int DFS(int u){
        int d1 = 0,d2 = 0;
        for (int i = h[u]; i != -1; i = ne[i]){
            int j = e[i];
            int d = DFS(j);
            //用数轴理解下面的两个if的更新
            if (d >= d1){
                d2 = d1;
                d1 = d;
            }else if (d > d2){
                d2 = d;
            }
        }
        res = Math.max(res,d1 + d2);
        return d1 + 1;
    }
    //加边函数
    static void add(int a,int b){
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
